package com.jld.managerialsystemforcovid19.controller;

import com.alibaba.fastjson.JSONObject;
import com.jld.managerialsystemforcovid19.TencentApi.HttpUtils;
import com.jld.managerialsystemforcovid19.entity.ChinaTotal;
import com.jld.managerialsystemforcovid19.service.ChinaTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller

public class ChinaTotalScheduleController {
    @Autowired
    private ChinaTotalService chinaTotalService;

    /**
     * 每小时执行一次，更新全国数据情况
     *
     * @throws Exception
     */
    @Scheduled(fixedDelay = 1000000)
    public void updateChinaTotalToDB() throws Exception {
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
        System.out.println("string:" + string);
        //所有数据的阿里巴巴格式数据
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");

        System.out.println(data.toString());
        System.out.println("====================================");
        //data
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object lastUpdateTime = jsonObjectData.get("overseaLastUpdateTime");
        System.out.println(chinaTotal);
        //total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
        System.out.println(total);
        //全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");

        //为程序赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setConfirm((Integer) input);
        dataEntity.setConfirm((Integer) severe);
        dataEntity.setConfirm((Integer) heal);
        dataEntity.setConfirm((Integer) dead);
        dataEntity.setConfirm((Integer) suspect);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        dataEntity.setUpdateTime(simpleDateFormat.parse(String.valueOf(lastUpdateTime)));

        //插入数据库
        chinaTotalService.save(dataEntity);
    }
}
